{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import pandas as pd \n",
    "import numpy as np \n",
    "from itertools import product"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "countries = ['us','de','dk','fr','it','nl','se','sp']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>value</th>\n",
       "      <th>free</th>\n",
       "      <th>se</th>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>name</th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>sigma</th>\n",
       "      <td>2.097183</td>\n",
       "      <td>True</td>\n",
       "      <td>0.010304</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>beta</th>\n",
       "      <td>0.970000</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>phi</th>\n",
       "      <td>0.304140</td>\n",
       "      <td>True</td>\n",
       "      <td>0.135992</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>psi</th>\n",
       "      <td>0.168902</td>\n",
       "      <td>True</td>\n",
       "      <td>0.019482</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>delta_h1</th>\n",
       "      <td>-0.967380</td>\n",
       "      <td>True</td>\n",
       "      <td>0.160692</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>delta_h2</th>\n",
       "      <td>3.487244</td>\n",
       "      <td>True</td>\n",
       "      <td>0.051008</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>eta</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>tfp</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>price</th>\n",
       "      <td>1.000000</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>risk</th>\n",
       "      <td>0.000000</td>\n",
       "      <td>False</td>\n",
       "      <td>NaN</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "             value   free        se\n",
       "name                               \n",
       "sigma     2.097183   True  0.010304\n",
       "beta      0.970000  False       NaN\n",
       "phi       0.304140   True  0.135992\n",
       "psi       0.168902   True  0.019482\n",
       "delta_h1 -0.967380   True  0.160692\n",
       "delta_h2  3.487244   True  0.051008\n",
       "eta       0.000000  False       NaN\n",
       "tfp       1.000000  False       NaN\n",
       "price     1.000000  False       NaN\n",
       "risk      0.000000  False       NaN"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "test = pd.read_pickle('output/params_ref_us.pkl')\n",
    "test"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [],
   "source": [
    "pars_select = ['delta_h1','delta_h2','price','tfp']\n",
    "stat = ['par','se']\n",
    "tuples = list(product(*[pars_select,stat]))\n",
    "table = pd.DataFrame(index=pd.MultiIndex.from_tuples(tuples),columns=countries)\n",
    "for c in countries: \n",
    "\tdf = pd.read_pickle('output/params_ref_'+c+'.pkl')\n",
    "\tfor p in pars_select:\n",
    "\t\ttable.loc[(p,'par'),c] = df.loc[p,'value']\n",
    "\t\ttable.loc[(p,'se'),c] = df.loc[p,'se']\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "pars_select = ['delta_h1','delta_h2','price','tfp']\n",
    "labels = ['$\\\\alpha_{10}$','$\\\\alpha_{11}$','$\\\\frac{p}{p_{US}}$','$\\\\frac{A}{A_{US}}$']\n",
    "maps_labels = dict(zip(pars_select,labels))\n",
    "table.index = pd.MultiIndex.from_tuples(list(product(*[labels,['','se']])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [],
   "source": [
    "for c in table.columns:\n",
    "\ttable[c] = table[c].astype('float64')\n",
    "\ttable[c] = table[c].round(3)\n",
    "\ttable[c] = table[c].astype('str')\n",
    "\ttable[c] = np.where(table[c]=='nan','-',table[c])\n",
    "\tfor s in table.index:\n",
    "\t\tif s[1]=='se':\n",
    "\t\t\tif table.loc[s,c]!='-':\n",
    "\t\t\t\tfmt = \"({se})\"\n",
    "\t\t\t\ttable.loc[s,c] = fmt.format(se=table.loc[s,c])\n",
    "table.index = pd.MultiIndex.from_tuples(list(product(*[labels,['','']])))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/html": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th></th>\n",
       "      <th>us</th>\n",
       "      <th>de</th>\n",
       "      <th>dk</th>\n",
       "      <th>fr</th>\n",
       "      <th>it</th>\n",
       "      <th>nl</th>\n",
       "      <th>se</th>\n",
       "      <th>sp</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">$\\alpha_{10}$</th>\n",
       "      <th></th>\n",
       "      <td>-0.967</td>\n",
       "      <td>-1.296</td>\n",
       "      <td>-1.601</td>\n",
       "      <td>-1.094</td>\n",
       "      <td>-0.716</td>\n",
       "      <td>-1.265</td>\n",
       "      <td>-1.525</td>\n",
       "      <td>-0.006</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <td>(0.161)</td>\n",
       "      <td>(0.098)</td>\n",
       "      <td>(0.01)</td>\n",
       "      <td>(0.013)</td>\n",
       "      <td>(0.032)</td>\n",
       "      <td>(0.03)</td>\n",
       "      <td>(0.011)</td>\n",
       "      <td>(0.008)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">$\\alpha_{11}$</th>\n",
       "      <th></th>\n",
       "      <td>3.487</td>\n",
       "      <td>3.954</td>\n",
       "      <td>4.273</td>\n",
       "      <td>3.698</td>\n",
       "      <td>3.868</td>\n",
       "      <td>3.998</td>\n",
       "      <td>4.311</td>\n",
       "      <td>3.394</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <td>(0.051)</td>\n",
       "      <td>(0.031)</td>\n",
       "      <td>(0.015)</td>\n",
       "      <td>(0.023)</td>\n",
       "      <td>(0.029)</td>\n",
       "      <td>(0.141)</td>\n",
       "      <td>(0.019)</td>\n",
       "      <td>(0.02)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">$\\frac{p}{p_{US}}$</th>\n",
       "      <th></th>\n",
       "      <td>1.0</td>\n",
       "      <td>0.847</td>\n",
       "      <td>0.888</td>\n",
       "      <td>0.615</td>\n",
       "      <td>0.713</td>\n",
       "      <td>0.698</td>\n",
       "      <td>0.844</td>\n",
       "      <td>0.65</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <td>-</td>\n",
       "      <td>(0.012)</td>\n",
       "      <td>(0.015)</td>\n",
       "      <td>(0.005)</td>\n",
       "      <td>(0.18)</td>\n",
       "      <td>(0.021)</td>\n",
       "      <td>(0.026)</td>\n",
       "      <td>(0.03)</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th rowspan=\"2\" valign=\"top\">$\\frac{A}{A_{US}}$</th>\n",
       "      <th></th>\n",
       "      <td>1.0</td>\n",
       "      <td>1.009</td>\n",
       "      <td>1.255</td>\n",
       "      <td>0.924</td>\n",
       "      <td>0.639</td>\n",
       "      <td>1.01</td>\n",
       "      <td>0.788</td>\n",
       "      <td>0.811</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th></th>\n",
       "      <td>-</td>\n",
       "      <td>(0.099)</td>\n",
       "      <td>(0.008)</td>\n",
       "      <td>(0.026)</td>\n",
       "      <td>(0.007)</td>\n",
       "      <td>(0.078)</td>\n",
       "      <td>(0.003)</td>\n",
       "      <td>(0.034)</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "text/plain": [
       "                          us       de       dk       fr       it       nl  \\\n",
       "$\\alpha_{10}$         -0.967   -1.296   -1.601   -1.094   -0.716   -1.265   \n",
       "                     (0.161)  (0.098)   (0.01)  (0.013)  (0.032)   (0.03)   \n",
       "$\\alpha_{11}$          3.487    3.954    4.273    3.698    3.868    3.998   \n",
       "                     (0.051)  (0.031)  (0.015)  (0.023)  (0.029)  (0.141)   \n",
       "$\\frac{p}{p_{US}}$       1.0    0.847    0.888    0.615    0.713    0.698   \n",
       "                           -  (0.012)  (0.015)  (0.005)   (0.18)  (0.021)   \n",
       "$\\frac{A}{A_{US}}$       1.0    1.009    1.255    0.924    0.639     1.01   \n",
       "                           -  (0.099)  (0.008)  (0.026)  (0.007)  (0.078)   \n",
       "\n",
       "                          se       sp  \n",
       "$\\alpha_{10}$         -1.525   -0.006  \n",
       "                     (0.011)  (0.008)  \n",
       "$\\alpha_{11}$          4.311    3.394  \n",
       "                     (0.019)   (0.02)  \n",
       "$\\frac{p}{p_{US}}$     0.844     0.65  \n",
       "                     (0.026)   (0.03)  \n",
       "$\\frac{A}{A_{US}}$     0.788    0.811  \n",
       "                     (0.003)  (0.034)  "
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "table"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [],
   "source": [
    "table.to_latex('../tables/table_8_country_specific_estimates.tex')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Average US-EU differences\n",
    "\n",
    "This number is used in counterfactual analysis. "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.7507142857142857"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "table.index = pd.MultiIndex.from_tuples(list(product(*[labels,['p','se']])))\n",
    "prices = table.loc[('$\\\\frac{p}{p_{US}}$','p'),:]\n",
    "prices['de':].astype('float64').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.33206470028544244"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1/prices['de':].astype('float64').mean()-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.9194285714285714"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "table.index = pd.MultiIndex.from_tuples(list(product(*[labels,['p','se']])))\n",
    "prices = table.loc[('$\\\\frac{A}{A_{US}}$','p'),:]\n",
    "prices['de':].astype('float64').mean()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "0.08763206960845249"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1/prices['de':].astype('float64').mean()-1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "interpreter": {
   "hash": "cf2a50979671a58939829e6829efb726aa5da11149213b77bd50351f899d04fb"
  },
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
